package com.itheima.demo;

import com.itheima.domain.Items;
import com.itheima.domain.Orders;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws IOException {
        //获取核心配置文件
        InputStream is = Resources.getResourceAsStream("sql.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //创建SqlSession对象
        SqlSession session = factory.openSession();
        //反射获取UserMapper接口
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        for (User user : all) {
            System.out.println("用户:" + user.getName());
            List<Orders> orders = user.getOrders();
            for (Orders order : orders) {
                System.out.println("订单编号:" + order.getNumber());
                List<Items> itemsList = order.getItemsList();
                for (Items items : itemsList) {
                    System.out.println("商品信息" + items.getName());
                }
            }
        }
        session.commit();
        session.close();
    }
}
